package helper.graph

import com.mxgraph.layout.mxCircleLayout
import com.mxgraph.swing.mxGraphComponent
import helper.base.LHMHelper.A3LHM
import org.jgrapht.Graph
import org.jgrapht.ext.JGraphXAdapter
import org.jgrapht.graph.DefaultDirectedGraph
import org.jgrapht.graph.DefaultEdge
import javax.swing.JFrame

object JGraphHelper {
    fun <A1, A2, A3> visualizeGraph(
        locationEventLocationLHM: A3LHM<A1, A2, A3>
    ) {
        // 创建一个有向图
        val graph: Graph<Any, DefaultEdge> = DefaultDirectedGraph(DefaultEdge::class.java)

        // 添加顶点和边

        locationEventLocationLHM.touch { a1, a2, a3 ->
            graph.addVertex(a1)
            graph.addVertex(a3)
            graph.addEdge(a1, a3)
        }

        // 创建一个适配器
        val jgxAdapter = JGraphXAdapter(graph)

        // 创建一个布局
        val layout = mxCircleLayout(jgxAdapter)

        // 应用布局
        layout.execute(jgxAdapter.defaultParent)

        // 创建一个 Swing 窗口来显示图
        val frame = JFrame()
        frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
        frame.setSize(400, 400)

        val graphComponent = mxGraphComponent(jgxAdapter)
        frame.contentPane.add(graphComponent)
        frame.isVisible = true
    }
}